package com.kadapatha.service.dao.user;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import com.kadapatha.domain.user.User;

@Repository
public class UserDAO extends HibernateDaoSupport {

    @Autowired
    public void init(SessionFactory factory) {
	setSessionFactory(factory);
    }

    public List<User> getAllUsers() {
	return getHibernateTemplate().find("From User");
    }

    public void save(User user) {
	getHibernateTemplate().save(user);
    }

    public boolean isExisting(String username) {
	List<User> user = getHibernateTemplate().find(
		"from User u where u.userName=?", username);
	return !user.isEmpty();
    }

    public List<User> findUserLike(User user) {
	return getHibernateTemplate().findByExample(user);
    }

    public boolean isAuthanticUser(String userName, String password,
	    boolean active) {
	return !getHibernateTemplate().find(
		"from User u where u.userName=? "
			+ "and u.password=? and u.active=?", userName, password,
		active).isEmpty();
    }

}
